/*
 * create by Littleken at 2019/1/14 10:02
 */
package com.littleken.product.VO;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 页面json返回，extends JsonData
 */
@Data
public class ResultVO  implements Serializable {//可序列化，用于页面缓存

    private static final long serialVersionUID = 4618155394459075029L;

    public static final Integer SUCCESS = 0;
    public static final Integer FAIL = 1;

    //返回结果 0成功 1失败
    private Integer code;
    //返回信息
    private String msg;
    //返回数据
    private Object data;

    private ResultVO(Integer code) {
        this.code = code;
    }

    //成功返回object与msg
    public static ResultVO success(Object object, String msg) {
        ResultVO resultVO = new ResultVO(SUCCESS);
        resultVO.data = object;
        resultVO.msg = msg;
        return resultVO;
    }

    //成功返回object
    public static ResultVO success(Object object) {
        ResultVO resultVO = new ResultVO(SUCCESS);
        resultVO.msg = "success";
        resultVO.data = object;
        return resultVO;
    }

    //空成功返回
    public static ResultVO success() {
        ResultVO resultVO = new ResultVO(SUCCESS);
        resultVO.setMsg("success");
        return resultVO;
    }

    //失败返回msg
    public static ResultVO fail(String msg) {
        ResultVO resultVO = new ResultVO(FAIL);
        resultVO.msg = msg;
        return resultVO;
    }

    //把json数据转回成map格式返回
    //由于ModelAndView需要传入Map型参数列表，故加入此方法
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("code", code);
        result.put("msg", msg);
        result.put("data", data);
        return result;
    }

}
